package bom.example.common.log;

import aom.example.security.domain.SelfUserEntity;
import bom.example.common.system.domain.SysLog;
import bom.example.common.system.service.SysLogService;
import bom.example.common.utils.DateUtils;
import bom.example.common.utils.Requestutils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 * @author X.I.O
 * @title: LogResponseBodyAdvice
 * @projectName springboot
 * @description: TODO
 * @date 2021/10/29 18:24
 */
@Order(3)
@Slf4j
@ControllerAdvice
@Configuration
public class LogResponseBodyAdvice implements ResponseBodyAdvice<Object> {

    @Autowired
    private SysLogService sysLogService;

    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        HttpServletRequest req = Requestutils.getHttpServletRequest();
        ApiOperation api=returnType.getMethodAnnotation(ApiOperation.class);
        String apiName="未定义接口名";
        if(api!=null){
            apiName=api.value();
        }
        String urlPath = Requestutils.getRequestUrl(req);
        // 获取请求参数内容json字符串
        String paramData = JSON.toJSONString(req.getParameterMap(),
                SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue);
        //获取请求方法
        String metnod=req.getMethod();
        String ip=Requestutils.getCliectIp(req);
        Authentication a=SecurityContextHolder.getContext().getAuthentication();
        SysLog sysLog=new SysLog();
        if(a!=null){
            SelfUserEntity userDetails=(SelfUserEntity) a.getPrincipal();
            String usernamme=userDetails.getUsername();
            String realname=userDetails.getRealname();
            Long id=userDetails.getUserId();
            sysLog.setLogUserid(String.valueOf(id));
            sysLog.setLogUsername(usernamme);
            sysLog.setLogRealname(realname);
        }
        sysLog.setLogName(apiName);
        sysLog.setLogParam(paramData);
        sysLog.setLogMethod(metnod);
        sysLog.setLogPath(urlPath);
        sysLog.setLogCreatetime(new Date());
        sysLogService.save(sysLog);
        return body;
    }
}
